package com.lv.controller;

import com.alibaba.fastjson.JSONObject;
import com.lv.data.Result;
import com.lv.dto.SellerDto;
import com.lv.dto.UserDto;
import com.lv.enums.ResultEnum;
import com.lv.enums.ToKenEnum;
import com.lv.exception.LoginException;
import com.lv.model.Admin;
import com.lv.shiro.LoginToKen;
import com.lv.util.JwtUtil;
import com.lv.util.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
//@RequestMapping("LoginController")
@RestController
public class LoginController {

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("login")
    public Result login(@RequestParam String username, @RequestParam String password){
        // 1,获取subject
        Subject subject = SecurityUtils.getSubject();
        // 2,将用户信息封装到token中
        LoginToKen usernamePasswordToken = new LoginToKen(username, password, ToKenEnum.ADMIN);
        // 3,调用subject的login方法
        try {
            subject.login(usernamePasswordToken);

        } catch (UnknownAccountException e){
            return  Result.response(ResultEnum.NAME_NO);
        } catch (IncorrectCredentialsException e){
            return  Result.response(ResultEnum.PWD_ER);
        }

        //登录成功封装 jwt吧并将token返回给前端
        Admin admin = (Admin) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setId(admin.getId().toString());
        String token = jwtUtil.createJwt(loginUser.getId(), JSONObject.toJSONString(loginUser), "loginUser");
        return  Result.OK().setData(token);
    }

    @PostMapping("sellerlogin")
    public Result sellerLogin(@RequestParam String username, @RequestParam String password){
        // 1,获取subject
        Subject subject = SecurityUtils.getSubject();
        // 2,将用户信息封装到token中
        LoginToKen usernamePasswordToken = new LoginToKen(username, password,ToKenEnum.SELLER);
        // 3,调用subject的login方法
        try {
            subject.login(usernamePasswordToken);

        } catch (UnknownAccountException e){
            return  Result.response(ResultEnum.NAME_NO);
        } catch (IncorrectCredentialsException e){
            return  Result.response(ResultEnum.PWD_ER);
        } catch (LoginException e){
            return  Result.response(ResultEnum.PROCESS_DOWN);
        }

        //登录成功封装 jwt吧并将token返回给前端
        SellerDto sllerDto = (SellerDto) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(sllerDto.getSellerId());
        loginUser.setId(sllerDto.getName());
        String token = jwtUtil.createJwt(loginUser.getId(), JSONObject.toJSONString(loginUser), "loginUser");
        return  Result.OK().setData(token);
    }

    @PostMapping("uulogin")
    public Result userLogin(@RequestParam String username, @RequestParam String password){
        // 1,获取subject
        Subject subject = SecurityUtils.getSubject();
        // 2,将用户信息封装到token中
        LoginToKen usernamePasswordToken = new LoginToKen(username, password, ToKenEnum.USER);
        // 3,调用subject的login方法
        try {
            subject.login(usernamePasswordToken);

        } catch (UnknownAccountException e){
            return  Result.response(ResultEnum.NAME_NO);
        } catch (IncorrectCredentialsException e){
            return  Result.response(ResultEnum.PWD_ER);
        }

        //登录成功封装 jwt吧并将token返回给前端
        UserDto userDto = (UserDto) SecurityUtils.getSubject().getPrincipal();
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(userDto.getUsername());
        String str = String.valueOf(userDto.getId());
        loginUser.setId(str);
        String token = jwtUtil.createJwt(loginUser.getId(), JSONObject.toJSONString(loginUser), "loginUser");
        return  Result.OK().setData(token);
    }

}
